package com.jcl.recruit.elasticsearch.resume.logic;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.DateUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.elasticsearch.resume.model.*;
import com.jcl.recruit.elasticsearch.resume.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ResumeCrawlerExportLogic
 * @Author yyliu
 * @Date 2018/11/13 10:20
 * @Version 1.0
 **/
@Component
public class ResumeCrawlerExportLogic {

    @Autowired
    private SearchResumeBasicInfoLogic searchResumeBasicInfoLogic;

    @Autowired
    private SearchResumePersInfoLogic searchResumePersInfoLogic;

    @Autowired
    private SearchResumeSelfEvaluationLogic searchResumeSelfEvaluationLogic;

    @Autowired
    private SearchResumeJobIntentionLogic searchResumeJobIntentionLogic;

    @Autowired
    private SearchResumeWorkLogic searchResumeWorkLogic;

    @Autowired
    private SearchResumeEduLogic searchResumeEduLogic;

    @Autowired
    private SearchResumeProjectLogic searchResumeProjectLogic;

    @Autowired
    private ResumeExternalMsgLogic resumeExternalMsgLogic;

    public SearchResumeBasicInfo exportCrawlerResume(ResumeCrawlerExportParam resume) {

        SearchResumeBasicInfo searchResumeBasicInfo = initResumeBasicInfo(resume);
        Integer resumeId = searchResumeBasicInfo.getId();
        if (null != resumeId) {
            // 人员基本信息
            initResumePersInfo(resume.getBasicInfo(), resumeId);
            // 期望
            initResumeJobIntention(resume.getJobIntention(), resumeId);
            // 自我介绍
            initResumeSelfEvaluation(resume.getSelfEvaluation(), resumeId);
            // 工作经历
            initResumeWorks(resume.getWorks(), resumeId);
            // 教育经历
            initResumeEdus(resume.getEducations(), resumeId);
            // 工作经历
            initResumeProjects(resume.getProjects(), resumeId);

            // 外部扩展信息
            if (StringUtils.hasLength(resume.getExternalMsg())) {
                resumeExternalMsgLogic.changeExternalSearchResumeMsg(resumeId,
                        resume.getSourceSub(),
                        resume.getExternalMsg(),
                        resume.getExternalId());
            }
            return searchResumeBasicInfo;
        }
        return null;
    }

    private SearchResumeBasicInfo initResumeBasicInfo(ResumeCrawlerExportParam entity) {
        SearchResumeBasicInfo resumeBasicInfo = new SearchResumeBasicInfo();
        if (null == entity) {
            return resumeBasicInfo;
        }
        resumeBasicInfo.setApplyDepartment(entity.getApplyDepartment());
        resumeBasicInfo.setApplyPosition(entity.getApplyPosition());
        //resumeBasicInfo.setApplyStatus();
        //resumeBasicInfo.setSource();
        resumeBasicInfo.setSourceSub(entity.getSourceSub());
        resumeBasicInfo.setSearchKeyword(entity.getSearchKeyword());
        resumeBasicInfo.setApplyOtherJid(entity.getExternalId());
        String applyDateStr = entity.getApplyDateStr();
        Date applyDate = null;
        if (StringUtils.hasLength(applyDateStr)) {
            try {
                applyDate = DateUtils.charToDate(applyDateStr, DateUtils.DATE_FORMAT_YMD);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        resumeBasicInfo.setApplyDate(applyDate);
        String updateDateStr = entity.getUpdateDateStr();
        Date updateDate = null;
        if (StringUtils.hasLength(updateDateStr)) {
            try {
                updateDate = DateUtils.charToDate(updateDateStr, DateUtils.DATE_FORMAT_YMD);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.hasLength(entity.getOwnership())) {
            String[] arrs = entity.getOwnership().split(StringUtils.COMMA);
            resumeBasicInfo.setCorpCode(arrs[0]);
            resumeBasicInfo.setChannelAccount(arrs[1]);
        }
        resumeBasicInfo.setUpdateDate(updateDate);
        resumeBasicInfo.setTags(entity.getTags());
        resumeBasicInfo.setStatus(entity.getStatus());
        resumeBasicInfo.setCreateTime(new Date());
        searchResumeBasicInfoLogic.add(resumeBasicInfo);

        return resumeBasicInfo;
    }

    private SearchResumePersInfo initResumePersInfo(ResumePersInfo entity, Integer resumeId) {
        if (null == entity) {
            return null;
        }
        SearchResumePersInfo _entity = new SearchResumePersInfo();
        BeanUtils.copyProperties(entity, _entity);
        if (StringUtils.hasLength(entity.getBirthdayStr())) {
            Date birthday = null;
            try {
                birthday = DateUtils.charToDate(entity.getBirthdayStr(), DateUtils.DATE_FORMAT_YMD);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            _entity.setBirthday(birthday);
        }
        _entity.setResumeId(resumeId);
        searchResumePersInfoLogic.add(_entity);
        return _entity;
    }

    private SearchResumeJobIntention initResumeJobIntention(ResumeJobIntention entity, Integer resumeId) {
        if (null == entity) {
            return null;
        }
        SearchResumeJobIntention _entity = new SearchResumeJobIntention();
        BeanUtils.copyProperties(entity, _entity);
        _entity.setResumeId(resumeId);
        searchResumeJobIntentionLogic.add(_entity);
        return _entity;
    }

    private SearchResumeSelfEvaluation initResumeSelfEvaluation(ResumeSelfEvaluation entity, Integer resumeId) {
        if (null == entity) {
            return null;
        }
        SearchResumeSelfEvaluation _entity = new SearchResumeSelfEvaluation();
        BeanUtils.copyProperties(entity, _entity);
        _entity.setResumeId(resumeId);
        searchResumeSelfEvaluationLogic.add(_entity);
        return _entity;
    }

    private void initResumeWorks(List<ResumeWork> entityList, Integer resumeId) {
        if (CollectionUtils.isNotEmpty(entityList)) {
            List<SearchResumeWork> _entityList = new ArrayList<>();
            for (ResumeWork entity : entityList) {
                SearchResumeWork _entity = new SearchResumeWork();
                BeanUtils.copyProperties(entity, _entity);
                _entity.setResumeId(resumeId);
                _entityList.add(_entity);
            }
            searchResumeWorkLogic.batchInsert(_entityList);
        }
    }

    private void initResumeEdus(List<ResumeEdu> entityList, Integer resumeId) {
        if (CollectionUtils.isNotEmpty(entityList)) {
            List<SearchResumeEdu> _entityList = new ArrayList<>();
            for (ResumeEdu entity : entityList) {
                SearchResumeEdu _entity = new SearchResumeEdu();
                BeanUtils.copyProperties(entity, _entity);
                _entity.setResumeId(resumeId);
                _entityList.add(_entity);
            }
            searchResumeEduLogic.batchInsert(_entityList);
        }
    }

    private void initResumeProjects(List<ResumeProject> entityList, Integer resumeId) {
        if (CollectionUtils.isNotEmpty(entityList)) {
            List<SearchResumeProject> _entityList = new ArrayList<>();
            for (ResumeProject entity : entityList) {
                SearchResumeProject _entity = new SearchResumeProject();
                BeanUtils.copyProperties(entity, _entity);
                _entity.setResumeId(resumeId);
                _entityList.add(_entity);
            }
            searchResumeProjectLogic.batchInsert(_entityList);
        }
    }
}
